home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / RANDOM.ASM < prev    next >
Assembly Source File  |  1989-08-31  |  1KB  |  59 lines

  1.     dosseg
  2.     .model    small
  3.     .stack
  4. NULL    segment para public 'BEGDATA'    ; force paragraph
  5. NULL    ends                ; alignment in DGROUP
  6.  
  7.     .data
  8. dgroup_str struc
  9.   ran_num    dw    0    ; random number seed
  10.   ran_mod    dw    0FFEFh    ;  modulus    2^16-17
  11.   ran_mul    dw    0FFD9h    ;  multiplier    2^16-39
  12.   ran_inc    dw    0FFFFh    ;  increment    -1
  13. dgroup_str ends
  14.  
  15. dgroup_start    dgroup_str <>    ; allocate space for dgroup structure
  16.  
  17.     .code
  18.     extrn    exit_program:near,outchr_newline:near,outchr_unsigned:near
  19.  
  20. ;;    main
  21. ;
  22. main    proc
  23.     mov    ax,@data        ; compensate for MS-LINK
  24.     mov    ss,ax            ;  bug which fails to set
  25.     mov    sp,offset stack        ;  DGROUP relative SS:SP
  26.  
  27.     cld                ; "start up" code for this
  28.     mov    bp,offset dgroup_start    ;  memory model
  29.  
  30.     mov    cx,10            ; display some random numbers
  31. mai1:    call    random_number
  32.     call    outchr_unsigned
  33.     call    outchr_newline
  34.     loop    mai1
  35.  
  36.     mov    ax,ax
  37.     jmp    exit_program
  38. main    endp
  39.  
  40.  
  41. ;;    random number
  42. ;
  43. ;    exit    AX    random number
  44. ;
  45. random_number proc
  46.     push    dx            ; compute random number using
  47.     mov    ax,ran_num[bp]        ;  linear congruential method.
  48.     mul    ran_mul[bp]        ;  see Knuth: Seminumerical
  49.     add    ax,ran_inc[bp]        ;   Algorithms
  50.     adc    dx,0
  51.     div    ran_mod[bp]
  52.     xchg    ax,dx
  53.     mov    ran_num[bp],ax
  54.     pop    dx
  55.     ret
  56. random_number endp
  57.  
  58.     end    main
  59.